<template>
	<view class="cu-modal bottom-modal" :class="{'show':isShow}" @click="hide">
		<view class="cu-dialog">
			<view class="goods-data">
				<view class="thumb">
					<image :src="require('../../img/'+imgurl)"></image>
				</view>
				<view class="item">
					<view class="title">
						<text></text>
					</view>
					<view class="price">
						<text class="min">￥</text>
						<text class="max">{{price}}</text>
						<text class="min">.00</text>
					</view>
					<view class="inventory">
						<text>库存：{{num}}</text>
					</view>
				</view>
			</view>
			<view class="attr-size">
				<div>
				<view class="attr-list" v-for="(item,index) in Attribute" :key="index">
					<view class="title">
						<text>{{item.attr}}</text>
					</view>
					<view class="size-list">
						<div class="list" v-for="(value,idx) in item.content"
							:class="{'action':Attribute[index].index === idx}"
							@click.stop="onAttrSize(item,value,index,idx)" :key="idx">

							<text>{{value.attributeContext}}</text>
						</div>
					</view>
				</view>
				</div>
				<div>
				<view class="attr-list" v-for="(two,Attrindex) in  Attributetwo" :key="Attrindex">
					<view class="title">
						<text>{{two.attr}}</text>
					</view>
					<view class="size-list">
						<div class="list" v-for="(convalue,Attridx) in two.content"
							:class="{'action':Attributetwo[Attrindex].index === Attridx}"
							@click.stop="onAttrcontent(two,convalue,Attrindex,Attridx)" :key="Attridx">
							<text>{{convalue.attributeContext}}</text>
						</div>
					</view>
				</view>
				</div>
				<view class="attr-number" @click.stop="onStop">
					<view class="tit">数量</view>
					<view class="number">
						<text class="iconfont icon-jian" @click="ondeduct()"></text>
						<input type="number" v-model="Shoppnum" disabled="disabled">
						<text class="iconfont icon-jia" @click="onadd()"></text>
					</view>
				</view>
			</view>
			<view class="attr-btn">
				<view class="add-cart" v-if="BuyType === 1" @click="oncart(cgomarr)">加入购物车</view>
				<view class="add-buy" v-if="BuyType === 0" @click="onConfirm(BuyType)">立即购买</view>
				<view class="confirm" v-if="BuyType === 2 || BuyType === 3" @click="onConfirm(cgomarr)">确定</view>
			</view>
		</view>
	</view>
</template>

<script>
	import axios from "axios"
	export default {
		data() {
			return {
				isShow: false,
				AttrIndex: 0,
				
				airter:12,
				SizeIndex: 0,
				price: 0,
				imgurl: "a",
				num: 0,
				Shoppnum: 1,
				// 购买类型
				BuyType: 0,
				Attribute: [],
				Attributetwo: [],
				contentGood:"1",
				contentGood1:"2",
				cgomarr: [
				 {
					Goodid:'1',
					GoodName: '',
					Goodsimgurl: '',
					AttrList:[],
					// Attrtype: '',
					// Attrtype1: '',
					Goodnum: '',
					Goodprice: '',
				},		
				],
				Goodcart:[
					{
						userId:'',
						goodId:'',
						goodAttr:[],				
					}
				],
				// goods:{
				//  GoodName:this.goodnamea,
				//  Goodsimgurl:this.imgurl,
				//  Attrtype:this.Attribute.content,
				//  Goodnum: this.Shoppnum,
				//  Goodprice:this.price,

				// }
			};
		},
		// created() {
		// 	axios.request("http://localhost:8089/Attr/SelAttr/"+1).then(
		// 		(resp) => {
		// 			console.log(resp)
		// 		}
		// 	)
		// 	},

		methods: {

			/**
			 * 显示 
			 * @param {Number} type 1 点击选择 2 加入购物 3 立即购买
			 */
			show(type, dat, dat1, goods) {
				this.BuyType = type;
				this.isShow = true;
				this.Attribute = dat;
				this.Attributetwo = dat1;
				this.price = this.Attributetwo[0].content[0].price;
				this.imgurl = this.Attributetwo[0].content[0].picurl;
				this.num = this.Attributetwo[0].content[0].num;
				this.cgomarr[0].GoodName = goods.goodName;
				this.cgomarr[0].Goodnum=this.Shoppnum;
				alert(this.cgomarr[0].Goodnum)
				if(this.cgomarr[0].AttrList.length==0){
				this.cgomarr[0].AttrList.push(this.Attribute[0].content[0].attributeContext);
				this.cgomarr[0].AttrList.push(this.Attributetwo[0].content[0].attributeContext);
				}
				this.cgomarr[0].Goodsimgurl=this.Attributetwo[0].content[0].picurl;
				this.cgomarr[0].Goodprice=this.Attributetwo[0].content[0].price;
			},
			imgError(item) {
				item.img = require('../../img/' + this.imgurl1);
			},
			hide() {
				this.isShow = false;
			},
			onStop() {

			},
			onadd() {
			
				this.Shoppnum += 1
			
			},
			ondeduct() {
				this.Shoppnum -= 1
				
				if (this.Shoppnum <= 1) {
					this.Shoppnum = 1
				}
				
			},
			oncart(type) {
				this.cgomarr[0].Goodnum=this.Shoppnum;
				alert(this.cgomarr[0].Goodnum)
				this.$queue.setData("GoodType",type)
				this.Goodcart[0].userId=this.$queue.getData("id")
				this.Goodcart[0].goodId=this.cgomarr[0].Goodid
				this.Goodcart[0].goodAttr=this.cgomarr[0].AttrList
				uni.request({
					url: "http://localhost:8089/Tro/insert/",
					header: {
						'content-type': 'application/json'
					},
					data: {
							userId:this.$queue.getData("id"),
							goodId:this.cgomarr[0].Goodid,
							goodAttr:this.cgomarr[0].AttrList,	
							goodNum:this.cgomarr[0].Goodnum,
							price:this.cgomarr[0].Goodprice,
					},
					method: 'post',
					success: function(res) {
						console.log(res)
					        uni.switchTab({
					        	url: '/pages/cart/cart',	 
								})
					   }  		
				})
				// console.log(CgoGT)
				// uni.switchTab({
				// 	url: '/pages/cart/cart',				
					
				// })
			},
			tocolor(a) {
				alert("111111")
			},
			/**
			 * 属性选择点击
			 */
			onAttrSize(item, value, index, idx) {
				alert(this.Attribute[index].content[idx].attributeContext)
				axios.request("http://localhost:8089/Attr/SelTwo/" + this.Attribute[index].content[idx].attributeContext +
					"/" + 1).then((res) => {
					this.Attributetwo = res.data
					console.log(res)
				})
				// alert(idx)
				this.Attribute[index].index = idx;
				this.AttrIndex = item.index;
				this.SizeIndex = value.index;
				// alert(value.attributeContext)
				// alert(value.attributeContext)
				this.cgomarr[0].AttrList.splice(0,1,value.attributeContext)
				alert(this.cgomarr[0].AttrList)
				// this.imgurl1 = this.Attribute[0].content[index].picurl;
				this.price = value.price;
				this.cgomarr[0].Goodprice=value.price;
				this.num = value.num;
				this.cgomarr[0].Goodnum=value.num;
				this.imgurl = value.picurl;  
				this.cgomarr[0].Goodsimgurl=value.picurl;
				 
			},

			onAttrcontent(two, convalue, Attrindex, Attridx) {
				this.Attributetwo[Attrindex].index = Attridx;
				this.AttrIndex = two.index;
				this.SizeIndex = convalue.index;
				this.cgomarr[0].AttrList.splice(1,1,convalue.attributeContext);
				alert(this.cgomarr[0].AttrList)
				this.price = convalue.price;
				this.cgomarr[0].Goodprice=convalue.price;
				this.num = convalue.num;		
				this.imgurl = convalue.picurl;
				this.cgomarr[0].Goodsimgurl=convalue.picurl;
			},
			showAlert(parms, msg, time) {
				if (parms) {
					toastr.success(msg, 'Tips：');
					setTimeout(function() {
						toastr.clear();
					}, time)
				} else {
					toastr.error(msg, 'Tips：');
					setTimeout(function() {
						toastr.clear();
					}, time)
				}
			},
			/**
			 * 确认点击
			 */
			onConfirm(type){
				// alert(this.cgomarr)t
				// var onjs=JSON.stringify(type)
			
				uni.navigateTo({
					url: '/pages/ConfirmOrder/ConfirmOrder'
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	@import 'GoodsAttr.scss';
</style>
